<template>
  <div class='ele-body ele-body-card'>
    <a-card
      :bordered='true'
      :body-style="{padding: '20px'}">
      <a-form
        ref='gatewaytimeformRef'
        :model='gatewaytimeformModel'
        :rules="gatewaytimeRules"
        :label-col='{ md: { span: 7 }, sm: { span: 24 } }'
        :wrapper-col='{ md: { span: 17 }, sm: { span: 24 } }'
        @submit='pushHeartInterval'
      >
        <a-row :gutter='8'>
          <a-col :md='10' :sm='24' :xs='24'>
            <a-form-item label='遥控点位1:'>
              <a-select
                v-model:value="gatewaytimeformModel.remoteBit1"
                placeholder="请选择状态"
                style="width: 205px;"
              >
                <a-select-option value="0">分</a-select-option>
                <a-select-option value="1">合</a-select-option>
                <!--<a-select-option value="2">闪烁</a-select-option>-->
                <a-select-option value="3">开延迟关</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24' />
        </a-row>
        <a-row :gutter='8'>
          <a-col :md='10' :sm='24' :xs='24'>
            <a-form-item label='延迟时间(秒):' name='remoteBit1Time'>
              <a-input v-model:value='gatewaytimeformModel.remoteBit1Time' placeholder='请输入延迟时间，单位s' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='2' :sm='24' :xs='24'>
            <a-button type='primary' html-type='submit' :loading='gatewaytimeformRefLoading'>
              提交
            </a-button>
          </a-col>
          <a-col :md='1' :sm='24' :xs='24'/>
          <a-col :md='11' :sm='24' :xs='24' />
        </a-row>
      </a-form>
    </a-card>

  </div>
</template>

<script>
import {DeviceCommandDownQueueApi} from '@/api/property/DeviceCommandDownQueueApi';
import { message } from 'ant-design-vue';

export default {
  name: 'DeviceLuoxunSimplephaseConfigOther',
  components: {
  },
  props: {
    data: Object
  },
  async created() {
    await this.init();
  },
  data() {
    return {
      loading : false,

      gatewaytimeformModel : {},
      gatewaytimeRules : {},
      gatewaytimeformRefLoading : false,
      gatewaytimeformRefTimer : undefined
    };
  },
  methods: {
    async init() {
      //this.gatewaytimeformModel.heartInterval = this.data.heartIntervalTime;
    },

    clearGatewaytimeformRef(){
      this.gatewaytimeformRefLoading = false;
      clearInterval(this.gatewaytimeformRefTimer);
    },

    checkProcess(dictate, clearCallback, callback) {
      let params = {
        recordId: this.data.recordId,
        sceneDictate: dictate
      };
      let result = DeviceCommandDownQueueApi.workingDetail(params);
      result
        .then(result => {
          if (!result || !result.dcdqId) { //处理完成了
            clearCallback && clearCallback(); //去掉菊花+清除循环调用
            callback && callback(); //用户自定义
          }
        })
        .catch(() => {
          clearCallback && clearCallback(); //去掉菊花+清除循环调用
        });
    },

    checkHeartInterval(){
      let that = this;
      this.checkProcess(1, this.clearGatewaytimeformRef, function(){
        that.gatewaytimeformModel.remoteBit1 = undefined;
        that.gatewaytimeformModel.remoteBit1Time = undefined;
        that.init();
      });
    },

    pushHeartInterval() {
      if (this.gatewaytimeformModel.remoteBit1 === undefined || this.gatewaytimeformModel.remoteBit1 === '') {
        message.error('请先选择遥控点位1状态');
        return;
      }
      if (this.gatewaytimeformModel.remoteBit1 == 3 && !this.gatewaytimeformModel.remoteBit1Time) {
        message.error('请先填写延迟时间');
        return;
      }

      this.loading = true;
      let updateDataRecord = {
        recordId: this.data.recordId,
        templateCode: 'luoxun1phase',
        updateData: JSON.stringify({ old: '', update: {remote_bit1 : this.gatewaytimeformModel.remoteBit1, remote_bit1_time : this.gatewaytimeformModel.remoteBit1Time}}),
        dictate : 1,
        sceneDictate : 1
      };
      let result = DeviceCommandDownQueueApi.add(updateDataRecord);
      result
        .then(result => {
          this.loading = false;
          message.success(result.message);

          this.gatewaytimeformRefLoading = true; //转圈
          this.gatewaytimeformRefTimer = setInterval(this.checkHeartInterval, 5000);
        })
        .catch(() => {
          this.loading = false;
        });
    }

  }
};
</script>
<style scoped>
/** 用户卡片 */
.workplace-user-card .ele-cell-content {
  overflow: hidden;
}

.workplace-user-card h4 {
  margin-bottom: 6px;
}
</style>
